<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

/**
 * @version		$Id: contact.php 10381 2008-06-01 03:35:53Z pasamio $
 * @package		Joomla
 * @subpackage	JobBoard
 * @copyright	Copyright (C) 2008 Joom Solutions. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant to the
 * GNU General Public License, and as distributed it includes or is derivative
 * of works licensed under the GNU General Public License or other free or open
 * source software licenses. See COPYRIGHT.php for copyright notices and
 * details.
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );


/**
 * @package		Joomla
 * @subpackage	JobBoard
 */
class JobBoardTableJA_Salary extends JTable
{
	/** @var int Primary key */
	var $id				= null;
	/** @var string */					
	var $value 			= null;
	/** @var int */
	var $ordering		= null;
	/** @var string */  
	var $params		= null;
	/** @var string */  
	var $published		= null;
	/** @var string */  
	var $checked_out		= null;
	/** @var string */  
	var $checked_out_time	= null;
	
	/**
	* @param database A database connector object
	*/
	function __construct(&$db)
	{
		parent::__construct( '#__ja_salary', 'id', $db );
	}
	
	/**
	 * Overloaded check function
	 *
	 * @access public
	 * @return boolean
	 * @see JTable::check
	 * @since 1.5
	 */
	function check()
	{		
		JFilterOutput::objectHTMLSafe($this,ENT_QUOTES,'');
		if(empty($this->value))
			return JText::_('Value empty');
			
		$where = '';
		if(!empty($this->id))
		{
			$where = ' and id != '.$this->id;
		}
		
		$query = "SELECT * FROM #__ja_salary WHERE upper(value) = upper('".$this->value."')".$where;
		
		$db = &JFactory::getDBO();
		$db->setQuery($query);
				
		$result = $db->loadObjectList();
		
		if(count($result) >0)
			return JText::_("Salary value already exist");
		
		return 'SUCCESS';
	}
	
	function reorder($filter_table){
		$k = $this->_tbl_key;
		$query = 'SELECT k.'.$this->_tbl_key.', k.ordering'
			. ' FROM '. $this->_tbl .' AS k'
			. ' WHERE k.ordering >= 0 '
			. ' ORDER BY  k.ordering'
			;
		$this->_db->setQuery( $query );
		if (!($orders = $this->_db->loadObjectList()))
		{
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		// compact the ordering numbers

		for ($i=0, $n=count( $orders ); $i < $n; $i++)
		{
			
			if ($orders[$i]->ordering >= 0)
			{
				if ($orders[$i]->ordering != $i+1)
				{
					$orders[$i]->ordering = $i+1;
					$query = 'UPDATE '.$this->_tbl
					. ' SET ordering = '. (int) $orders[$i]->ordering
					. ' WHERE '. $k .' = '. $this->_db->Quote($orders[$i]->$k)
					;
					$this->_db->setQuery( $query);
					$this->_db->query();
				}
			}
		}

		return true;
	}
}